package com.iwuke.dance.wallet;

import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;
import com.iwuke.dance.R;
import com.iwuke.dance.activity.BaseActivity;
import com.iwuke.dance.gadget_view.TitleBarView;
import com.iwuke.dance.http_request.PathUrl;
import com.iwuke.dance.util.RadioGroupUtil;
import com.iwuke.dance.util.SharedPreferencesUtil;
import com.iwuke.dance.util.ToastUtil;
import com.iwuke.dance.util.Util;
import com.iwuke.dance.util.merchants_connect.NetworkEvent;
import com.iwuke.dance.wallet.balance.BalanceActivity;
import com.iwuke.dance.wallet.bean.QueryWalletBean;
import com.iwuke.dance.wallet.e_coin.MyECoinActivity;
import com.iwuke.dance.wallet.fragment.BankCardFragment;
import com.iwuke.dance.wallet.fragment.CouponFragment;
import com.iwuke.dance.wallet.mind_value.MindValueActivity;
import com.iwuke.dance.wallet.settings.WalletSettingsActivity;
import com.iwuke.dance.wallet.starlight_value.MyStarlightValueActivity;

import java.util.HashMap;
import java.util.Map;

import androidx.viewpager.widget.ViewPager;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * @author: 魏从响
 * @date: 2025/9/16
 * 钱包
 */
public class WalletActivity extends BaseActivity implements View.OnClickListener {


    private TitleBarView title_bar;
    private ImageView is_show;
    private RelativeLayout rlt_money;
    private TextView tv_money;
    private LinearLayout llt_e_coin;
    private LinearLayout llt_starlight_value;
    private LinearLayout llt_heart_value;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private String[] title = {"优惠券(0)", "银行卡(0)"};
    private CouponFragment couponFragment;
    private BankCardFragment bankCardFragment;

    private RadioGroup rgp_record_successful;
    private FrameLayout flt_record_successful;
    private RadioButton rbn_coupon, rbn_bank_card;
    private RadioGroupUtil radioGroupUtil;
    private View v_bottom_1;
    private View v_bottom_2;
    private TextView tv_add_band_card;
    private TextView tv_e_coin;
    private TextView tv_starlight_value;
    private TextView tv_heart_value;
    private String balance = "0.0";
    private String ecurrency = "0";
    private String starlightValue = "0";
    private String mindValue = "0";
    private boolean isShow = true;//true 显示；false 隐藏

    /**
     * @description 网络实时监听
     */
    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onEvent(NetworkEvent networkEvent) {
        BaseActivity.setConnect(WalletActivity.this, networkEvent, title_bar);

    }

    @Override
    protected int getView() {
        return R.layout.activity_wallet;
    }

    @Override
    protected void init(View mView) {

        title_bar = findViewById(R.id.title_bar);

        //是否显示
        is_show = findViewById(R.id.is_show);
        //点击跳转详情
        rlt_money = findViewById(R.id.rlt_money);
        //钱/余额
        tv_money = findViewById(R.id.tv_money);
        //余额
        tv_money.setText(balance+"元");
        //E币
        llt_e_coin = findViewById(R.id.llt_e_coin);
        tv_e_coin = findViewById(R.id.tv_e_coin);
        //E币
        tv_e_coin.setText(ecurrency);
        //星光值
        llt_starlight_value = findViewById(R.id.llt_starlight_value);
        tv_starlight_value = findViewById(R.id.tv_starlight_value);
        //星光值
        tv_starlight_value.setText(starlightValue);
        //心意值
        llt_heart_value = findViewById(R.id.llt_heart_value);
        tv_heart_value = findViewById(R.id.tv_heart_value);
        //心意值
        tv_heart_value.setText(mindValue);

//        //tab
//        tabLayout = findViewById(R.id.tabLayout);
//        //子页面
//        viewPager = findViewById(R.id.viewPager);

        //切换点击事件
        rgp_record_successful = findViewById(R.id.radio_group);
        flt_record_successful = findViewById(R.id.frame_layout);


        //
        rbn_coupon = findViewById(R.id.radio_button_1);
        v_bottom_1 = findViewById(R.id.v_bottom_1);
        rbn_coupon.setText("优惠券(0)");
        //
        rbn_bank_card = findViewById(R.id.radio_button_2);
        v_bottom_2 = findViewById(R.id.v_bottom_2);
        rbn_bank_card.setText("银行卡(0)");
        //添加银行卡
        tv_add_band_card = findViewById(R.id.tv_add_band_card);


        title_bar.setTitleText("我的钱包");
        title_bar.setLeftOnClick(this);
        title_bar.setRightImageViewVisibility(true);
        title_bar.setRightImageViewColor(R.mipmap.img_settings);
        title_bar.setOnRightImageViewListener(this);
        //是否显示
        is_show.setOnClickListener(this);
        //点击跳转详情
        rlt_money.setOnClickListener(this);
        //E币
        llt_e_coin.setOnClickListener(this);
        //星光值
        llt_starlight_value.setOnClickListener(this);
        //心意值
        llt_heart_value.setOnClickListener(this);
        //添加银行卡
        tv_add_band_card.setOnClickListener(this);


    }

    @Override
    protected void initData() {

        //优惠券
        initCoupon();

        //银行卡
        initBankCard();

        radioGroupUtil = new RadioGroupUtil(WalletActivity.this, rbn_coupon, rbn_bank_card, v_bottom_1, v_bottom_2);

        radioGroupUtil.setRadioButtonColor(1);
        switchFragment(couponFragment);

        rgp_record_successful.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.radio_button_1) {
                    //优惠券
                    radioGroupUtil.setRadioButtonColor(1);
                    switchFragment(couponFragment);
                    tv_add_band_card.setVisibility(View.GONE);
                } else if (checkedId == R.id.radio_button_2) {
                    //银行卡
                    radioGroupUtil.setRadioButtonColor(2);
                    switchFragment(bankCardFragment);
                    tv_add_band_card.setVisibility(View.VISIBLE);
                }

            }
        });

//        viewPager.setAdapter(new Adapter(getSupportFragmentManager()));
//        tabLayout.setupWithViewPager(viewPager);
//
//        getItemView();
//        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
//            @Override
//            public void onTabSelected(TabLayout.Tab tab) {
//                setStatus(tab);
//            }
//
//            @Override
//            public void onTabUnselected(TabLayout.Tab tab) {
//
//            }
//
//            @Override
//            public void onTabReselected(TabLayout.Tab tab) {
//                setStatus(tab);
//            }
//        });

    }


    @Override
    protected void onResume() {
        super.onResume();
        if (TextUtils.isEmpty(SharedPreferencesUtil.getInstance().getPhoneNumber())){
            Util.setIntent(WalletActivity.this, OpenWalletActivity.class);
            return;
        }

        queryWallet();

    }

    private void queryWallet() {
        showLoadingDialog();
        Map<String, String> map = new HashMap<>();
        map.put("userId", SharedPreferencesUtil.getInstance().getUserId());//后期改成动态值
        PathUrl.queryByUserId(map, new PathUrl.DataCallBack<QueryWalletBean>() {
            @Override
            public void onSucess(QueryWalletBean bean) {
                dismissLoadingDialog();
//                LogUtil.logD("++++++++++json" ,new Gson().toJson(bean));
                if (bean != null) {
                    balance = String.valueOf(bean.balance);
                    ecurrency = String.valueOf(bean.ecurrency);
                    starlightValue = String.valueOf(bean.starlightValue);
                    mindValue = String.valueOf(bean.mindValue);

                    //余额
                    tv_money.setText(balance+"元");
                    //E币
                    tv_e_coin.setText(ecurrency);
                    //星光值
                    tv_starlight_value.setText(starlightValue);
                    //心意值
                    tv_heart_value.setText(mindValue);


                }

            }

            @Override
            public void onFail(String errorCode) {
                dismissLoadingDialog();
                ToastUtil.setToast(WalletActivity.this, errorCode);
            }
        });
    }

    /**
     * @description 优惠券
     * @author 魏从响
     * @time 2025/9/17 10:51
     */
    private void initCoupon() {
        if (couponFragment == null) {
            couponFragment = new CouponFragment();
            fragmentList.add(couponFragment);
        }
    }

    /**
     * @description 银行卡
     * @author 魏从响
     * @time 2025/9/17 10:51
     */
    private void initBankCard() {
        if (bankCardFragment == null) {
            bankCardFragment = new BankCardFragment();
            fragmentList.add(bankCardFragment);
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.img_left) {
            finish();
        } else if (v.getId() == R.id.img_add) {//钱包设置
            Util.setIntent(WalletActivity.this, WalletSettingsActivity.class);
        } else if (v.getId() == R.id.is_show) {
            //钱/金额是否显示
            if (isShow) {
                isShow = false;
                is_show.setImageResource(R.mipmap.img_hide);//闭眼
                tv_money.setText("****元");
            }else {
                isShow = true;
                is_show.setImageResource(R.mipmap.img_show);//睁眼
                tv_money.setText(balance+"元");
            }

        } else if (v.getId() == R.id.rlt_money) {
            //余额详情页面
            Util.setIntent(WalletActivity.this, BalanceActivity.class, balance);
        } else if (v.getId() == R.id.llt_e_coin) {
            //E币
            if (!TextUtils.isEmpty(ecurrency)) {
                Util.setIntent(WalletActivity.this, MyECoinActivity.class, ecurrency);
            }

        } else if (v.getId() == R.id.llt_starlight_value) {
            //星光值
            if (!TextUtils.isEmpty(starlightValue)) {
                Util.setIntent(WalletActivity.this, MyStarlightValueActivity.class, starlightValue);
            }

        } else if (v.getId() == R.id.llt_heart_value) {
            //心意值
            Util.setIntent(WalletActivity.this, MindValueActivity.class, mindValue);

        }else if (v.getId()==R.id.tv_add_band_card){
            //添加银行卡
            Util.setIntent(WalletActivity.this,AddBandCardActivity.class);
        }
    }

    /**
     * 获取Tablayout的自定义布局，并设置标题
     */
//    private void getItemView() {
//        for (int i = 0; i < tabLayout.getTabCount(); i++) {
//            View view = LayoutInflater.from(this).inflate(R.layout.item, null);
//            ((TextView) view.findViewById(R.id.tv)).setText(title[i]);
//            tabLayout.getTabAt(i).setCustomView(view);
//        }
//        ((TextView) tabLayout.getTabAt(0).getCustomView().findViewById(R.id.tv)).setTextColor(getResources().getColor(R.color.color_000));
////        tabLayout.getTabAt(0).getCustomView().findViewById(R.id.iv).setVisibility(View.VISIBLE);
//    }

    /**
     * 设置tablayout选中状态
     *
     * @param tab
     */
//    private void setStatus(TabLayout.Tab tab) {
//        for (int i = 0; i < tabLayout.getTabCount(); i++) {
//            if (i == tab.getPosition()) {
//                ((TextView) tabLayout.getTabAt(i).getCustomView().findViewById(R.id.tv)).setTextColor(getResources().getColor(R.color.color_000));
////                tabLayout.getTabAt(i).getCustomView().findViewById(R.id.iv).setVisibility(View.VISIBLE);
//            } else {
//                ((TextView) tabLayout.getTabAt(i).getCustomView().findViewById(R.id.tv)).setTextColor(getResources().getColor(R.color.color_666));
////                tabLayout.getTabAt(i).getCustomView().findViewById(R.id.iv).setVisibility(View.INVISIBLE);
//            }
//        }
//    }

    //为了省事儿Adapter写成内部类
//    class Adapter extends FragmentPagerAdapter {
//
//        public Adapter(FragmentManager fm) {
//            super(fm);
//        }
//
//        @Override
//        public Fragment getItem(int position) {
//            return fm.get(position);
//        }
//
//        @Override
//        public int getCount() {
//            return fm.size();
//        }
//
//        @Override
//        public CharSequence getPageTitle(int position) {
//            return title[position];
//        }
//    }


}
